<template>
	<view class="conent">
		<view class="list">
			<view class="giftCard-content" v-for="(item,index) in datasource" :key="index">
				<image class="line-bg" src="/static/images/img_yhq_blue.png"></image>
				<view class="mes-view">
					<view class="mes-left">
						<text style="color: #0A77DE;">¥{{item.face_value || ''}}</text>
						<view class="line-line"></view>
					</view>
					<view class="mes-right">
						<view class="row-center">
							<text class="name line1">{{item.card_name}}</text>
							<image class="select-icon" @click="selectItem(index)"
								:src="item.select?'../../../static/images/check_s.png':'../../../static/images/check_n.png'"
								mode="widthFix"></image>
						</view>
						<view class="row-center" style="margin-top: 58upx;">
							<text style="color: #666;font-size: 24upx;">有效期{{item.useful_days}}天</text>
							<view class="number-view">
								<view class="reduction" @click="reduction(index)">-</view>
								<input class="number-input" type="number" :value="item.number"
									@input='inputNumber($event,index)' @blur="inputEnd($event,index)" />
								<view class="reduction add" @click="addNuber(index)">+</view>
							</view>
						</view>

					</view>
				</view>
			</view>
			<view class="bottom-view">
				<view style="display: flex;align-items: center;">
					<text style="color: #333333;font-size: 28upx;">合计：</text>
					<text style="color: #FF0000;font-size: 26upx;">￥{{total}}</text>
				</view>
				<view class="next-btn" @click="nextAction" style="background-color: #0A77DE;">下一步</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getCardList
	} from '@/api/peopleHide.js'
	export default {
		data() {
			return {
				datasource: [],
				total: 0,
				joinType: 0
			}
		},
		onLoad(options) {
			if (options.joinType) {
				this.joinType = options.joinType
			}
			// this.$on("updatasource", () => {
			// 	console.log('监听到回调')
			// })
			this.getCardList()
		},
		
		methods: {
			calculateMoney() {
				let totalPrice = 0
				this.datasource.forEach(tem => {
					if (tem.select) {
						totalPrice = tem.number * tem.face_value + totalPrice
					}
				})
				this.total = totalPrice
			},
			getCardList() {
				getCardList({}).then(res => {
					res.data.data.forEach(tem => {
						tem.select = false
						tem.number = 1
					})
					this.datasource = res.data.data
				})
			},
			selectItem(e) {
				this.datasource[e].select = !this.datasource[e].select
				this.calculateMoney()
			},
			inputNumber(e, index) {
				let number = (e.detail.value == '' || e.detail.value == 0) ? 1 : e.detail.value;
				let index1 = index
				this.datasource[index1].number = number
				this.calculateMoney()
			},
			inputEnd(e, index) {
				let number = (e.detail.value == '' || e.detail.value == 0) ? 1 : e.detail.value;
				let index1 = index
				this.datasource[index1].number = number
				this.calculateMoney()
			},
			reduction(e) {
				let index = e
				if (this.datasource[index].number > 1) {
					this.datasource[index].number--
				}
				this.calculateMoney()
			},
			addNuber(e) {
				let index = e

				this.datasource[index].number++
				this.calculateMoney()

			},
			nextAction() { //下一步
				//整理数据
				let selectArr = []
				this.datasource.forEach(tem => {
					if (tem.select) {
						let dict = {
							card_id: tem.id,
							card_num: tem.number
						}
						selectArr.push(dict)
					}
				})
				if (selectArr.length == 0) {
					uni.showModal({
						title: '提示',
						content: '请选择需要购买的礼品卡',
						showCancel: false,
						success(res1) {
							if (res1.confirm) {

							}
						}
					})
				} else {
					uni.navigateTo({
						url: '../getCardNumber/index?pamadict=' + JSON.stringify(selectArr) + "&joinType=" + this
							.joinType
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.conent {
		padding-bottom: 140upx;
	}

	.giftCard-content {
		width: 710upx;
		margin: 20upx auto 0;
		background-color: #fff;
		display: flex;
		flex-direction: column;
		box-shadow: 0px -4upx 28upx 0px rgba(0, 0, 0, 0.06);
		box-sizing: border-box;
		border-radius: 10upx;
		overflow: hidden;

		.line-bg {
			width: 710upx;
			height: 20upx;
		}

		.mes-view {
			width: 100%;
			height: 208upx;
			background-color: #fff;
			padding-right: 30upx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;

			.mes-left {
				width: 198upx;
				height: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #FA6400;
				font-size: 48upx;
				font-weight: 600;
				position: relative;

				.line-line {
					position: absolute;
					color: #DCDCDC;
					width: 1px;
					height: 180upx;
					right: 0;
					border-right-style: dotted;
				}

				// border-right-style: dotted;
			}

			.mes-right {
				display: flex;
				flex-direction: column;
				height: 100%;
				flex: 1;
				margin-left: 30upx;
				justify-content: center;

				.row-center {
					display: flex;
					align-items: center;
					justify-content: space-between;
					// padding-right: 30upx;
					box-sizing: border-box;

					.name {
						color: #333333;
						font-size: 32upx;
						font-weight: 600;
					}

					.type {
						color: #FA6400;
						font-size: 28upx;
					}
				}
			}
		}
	}

	.select-icon {
		width: 32upx;
		height: 32upx;
	}

	.number-view {
		display: flex;
		align-items: center;
	}

	.reduction {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 48upx;
		height: 48upx;
		color: #333333;
		font-size: 46upx;
		font-weight: 600;
	}

	.number-input {
		width: 88upx;
		height: 48upx;
		opacity: 1;
		background: #f6f6f6;
		text-align: center;
		margin: 0 20upx;
	}

	.add {
		color: #0A77DE;
	}

	.bottom-view {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 120upx;
		background-color: #fff;
		z-index: 9;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30upx;
		box-sizing: border-box;

		.next-btn {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 240upx;
			height: 70upx;
			background: #e93323;
			border-radius: 36upx;
			color: #fff;
			font-size: 28upx;
		}
	}
</style>
